package com.aixin.csopuser.controller;


import com.aixin.common.core.domain.model.LoginUser;
import com.aixin.common.utils.ServletUtils;
import com.aixin.csopuser.domain.*;
import com.aixin.csopuser.domain.vo.PointHistory;
import com.aixin.csopuser.model.*;
import com.aixin.csopuser.service.CustomerService;
import com.aixin.csopuser.utils.ResponseUtils;
import com.aixin.framework.web.service.TokenService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
@EnableAutoConfiguration
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取客户相关信息列表（权益，保单，服务等）
     * @param customerID
     * @return
     */
    @GetMapping(value = "/full-info/{customerID}")
    public CustomerDomain getCustomer(@PathVariable String customerID) {
        return customerService.getFullInfo(customerID);
    }

    /**
     * 获取客户列表
     */
    @RequestMapping(value = "/getPageCustomer")
    public PageInfo getPageCustomer(CustomerModel customerModel, int pageNum, int pageSize) {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long deptID = loginUser.getUser().getDeptId();

        if (StringUtils.isEmpty(customerModel.getCustName())) {
            customerModel.setCustName(null);
        }
        if (StringUtils.isEmpty(customerModel.getServiceID())) {
            customerModel.setServiceID(null);
        }
        if (StringUtils.isEmpty(customerModel.getCertID())) {
            customerModel.setCertID(null);
        }
        if (StringUtils.isEmpty(customerModel.getEquityLevelID())) {
            customerModel.setEquityLevelID(null);
        }
        if (StringUtils.isEmpty(customerModel.getPhoneNumber())) {
            customerModel.setPhoneNumber(null);
        }
        return customerService.getCustomerDomain(customerModel, String.valueOf(deptID), true, pageNum, pageSize);
    }

    /**
     * 根据供应商获取客户列表
     */
    @PostMapping(value = "/getCustomerBySupplier")
    public PageInfo<SupplierCustomerDomain> postCustomerBySupplier(SupplierCustomerModel model, int pageNum, int pageSize) {
        if ("".equals(model.getSupplierID())) {
            model.setSupplierID(null);
        }

        if ("".equals(model.getServiceID())) {
            model.setServiceID(null);
        }
//        return customerService.postCustomerBySupplier(supplierCustomerModel,pageNum,pageSize);
        PageInfo result = customerService.getCustomerBySupplier(model, false, pageNum, pageSize);
        return result;
    }


    /**
     * 获取用户积分
     */
    @RequestMapping(value = "/getPoint")
    public ReturnPorint getPoint(String customerID) {
        ReturnPorint returnPorint = new ReturnPorint();
        returnPorint.setList(customerService.getPoint(customerID));
        return returnPorint;
    }

    /**
     * 用户积分记录
     * @param customerID
     * @return
     */
    @GetMapping(value = "/points/{customerID}")
    public ResponseEntity<List> getPoints(@PathVariable String customerID) {
        List list = customerService.getCustomerPointHistory(customerID);
        return ResponseEntity.ok().body(list);
    }


    /**
     * 获取用户权益
     */
    @RequestMapping(value = "/getEquity")
    public ReturnEquity getEquity(String customerID) {
        ReturnEquity returnEquity = new ReturnEquity();
        returnEquity.setList(customerService.getEquity(customerID));
        return returnEquity;
    }

    /**
     * 根据客户ID获取保单
     */
    @GetMapping(value = "/getPolicy/{customerID}")
    public ReturnEquity getPolicy(@PathVariable String customerID) {
        ReturnEquity returnEquity = new ReturnEquity();
        returnEquity.setList(customerService.getPolicy(customerID));
        return returnEquity;
    }

    /**
     * 获取客户服务
     */
    @GetMapping(value = "/getServices")
    public ReturnEquity getServices(CustServiceModel custServiceModel) {
        ReturnEquity returnEquity = new ReturnEquity();
        returnEquity.setList(customerService.getService(custServiceModel));
        return returnEquity;
    }

    /**
     * 获取客户服务
     */
    @GetMapping(value = "/services/policy/{policyID}")
    public ReturnEquity queryServices(@PathVariable String policyID) {
        ReturnEquity returnEquity = new ReturnEquity();
        returnEquity.setList(customerService.getServicesByPolicyNumber(policyID));
        return returnEquity;
    }

    /**
     * 获取服务流水
     */
    @RequestMapping(value = "/getServicesHistory")
    public List<Map<String, String>> getServicesHistory(@RequestBody ServicesHistoryModel servicesHistoryModel) {
        return customerService.getServicesHistory(servicesHistoryModel);
    }

    /**
     * 获取服务流水
     */
    @GetMapping(value = "/service-history")
    public List<ServiceConsumeHistory> getServiceConsumeHistory(ServicesHistoryModel model) {
        return customerService.findServicesConsumeHistory(model);
    }

    /**
     * 保存服务流水
     */
    @PostMapping(value = "/service-history")
    public SystemReturn createServiceConsumeHistory(@RequestBody ServicesHistory servicesHistory) {
        String historyNumber = customerService.saveServicesHistory(servicesHistory);
        if (historyNumber != null) {
            return SystemReturn.getSystemTrueReturn();
        }
        return SystemReturn.getSystemFalseReturn();
    }

    /**
     * 保存服务流水
     */
    @RequestMapping(value = "/saveServicesHistory")
    public SystemReturn saveServicesHistory(@RequestBody ServicesHistory servicesHistory) {
        String historyNumber = customerService.saveServicesHistory(servicesHistory);
        if (historyNumber != null) {
            return SystemReturn.getSystemTrueReturn();
        }
        return SystemReturn.getSystemFalseReturn();
    }

    /**
     * 修改服务流水
     */
    @RequestMapping(value = "/updateServicesHistory")
    public SystemReturn updateServicesHistory(@RequestBody ServicesHistory servicesHistory) {
        String historyNumber = customerService.updateServicesHistory(servicesHistory);
        if (historyNumber != null) {
            return SystemReturn.getSystemTrueReturn();
        }
        return SystemReturn.getSystemFalseReturn();
    }
}
